package com.fc.service.impl

import groovy.transform.TypeChecked

import org.hibernate.Criteria
import org.hibernate.Query
import org.hibernate.criterion.Projections
import org.hibernate.criterion.Restrictions
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional

import com.fc.entities.Activity
import com.fc.entities.ActivityGroup
import com.fc.entities.User
import com.fc.service.ActivityService
import com.fc.service.exceptions.FCException

/**
 * 
 * @author aacs
 *
 */
@Repository("activityService")
@Transactional
@TypeChecked
class ActivityServiceImpl extends BaseServiceImpl<Activity> implements ActivityService {
	Activity findByName(String name) {
		Criteria criteria = currentSession().createCriteria(Activity.class);
		Activity fg = (Activity) criteria.add(Restrictions.eq("name", name)).uniqueResult();
		return fg;
	}

	@Override
	List<String> findAllNames() {
		Criteria criteria = currentSession().createCriteria(Activity.class, "activity").setProjection(Projections.projectionList().add(Projections.property("activity.name")))
		return criteria.list()
	}

	@Override
	List<Activity> findAllByGroup(ActivityGroup group) {
		Criteria criteria = currentSession().createCriteria(Activity.class)
		return criteria.add(Restrictions.eq("activityGroup", group)).list()
	}

	@Override
	List<Activity> findAllByUser(User user) {
		Criteria criteria = currentSession().createCriteria(Activity.class)
		return criteria.add(Restrictions.eq("user", user)).list()
	}

	@Override
	List<Activity> findAllByGroupAndUser(ActivityGroup group, User user) {
		Criteria criteria = currentSession().createCriteria(Activity.class)
		criteria.add(Restrictions.eq("activityGroup", group))
		criteria.add(Restrictions.eq("user", user))
		return criteria.list()
	}

	@Override
	List<Activity> findAllDefault() {
		Query query = currentSession().createQuery("from Activity where user_id = :userid ");
		query.setParameter("userid", "1");
		List list = query.list()
		return list
	}

	@Override
	List<Activity> findAllDefaultByGroup(ActivityGroup group) {
		Query query = currentSession().createQuery("from Activity where user_id = :userid and activityGroup_id = :activityGroupid");
		query.setParameter("userid", "1");
		query.setParameter("activityGroupid", group.id);
		List list = query.list()
		return list
	}

	@Override
	Activity save(Activity activity) {
		try {
			return (Activity) super.save(activity);
		} catch (Throwable ex) {
			throw new FCException("Az adott gyakorlat (${activity.name}) módosítása nem lehetséges.")
		}
	}

	@Override
	void delete(Activity activity) {
		try {
			super.delete(activity);
		} catch (Exception ex) {
			throw new FCException("Az adott gyakorlat (${activity.name}) törlése nem lehetséges.")
		}
	}

	@Override
	Activity create(Activity activity) {
		try {
			return (Activity) super.create(activity);
		} catch (Exception ex) {
			throw new FCException("Az adott gyakorlat (${activity.name}) létrehozása nem lehetséges.")
		}
	}

	@Override
	List<Activity> findAll() {
		findAll(Activity.class)
	}
}
